import ColorUISdk from '../../mp-sdk/index'

const sdk = new ColorUISdk({
  env: 'dev',
  version: '1.0.0',
  api: {
    dev: { 
      // url: 'http://172.20.10.2:5000' 
      // url:'http://127.0.0.1:5000'
      url:'http://192.168.134.31:5000'
    }
  }
})

Page({
  data: {
    recommendedBuildings: [] as any[],
    newsList: [] as any[]
  },

  onLoad() {
    // 加载推荐建筑
    this.loadRecommendedBuildings()
    
    // 加载文化资讯
    this.loadNewsList()
  },

  // 加载推荐建筑
  async loadRecommendedBuildings() {
    try {
      const res = await sdk.request({
        url: '/api/building/recommended',
        method: 'GET'
      })
      
      if (res.code === 0) {
        this.setData({
          recommendedBuildings: res.data || []
        })
      }
    } catch (error) {
      console.error('加载推荐建筑失败:', error)
    }
  },

  // 加载文化资讯
  async loadNewsList() {
    try {
      const res = await sdk.request({
        url: '/api/news/list',
        method: 'GET'
      })
      
      if (res.code === 0) {
        this.setData({
          newsList: res.data || []
        })
      }
    } catch (error) {
      console.error('加载文化资讯失败:', error)
    }
  },

  // 前往建筑识别
  goToBuildingIdentify() {
    wx.navigateTo({
      url: '/pages/building-identify/building-identify'
    })
  },

  // 前往AR文化解说
  goToARCulture() {
    wx.navigateTo({
      url: '/pages/ar-culture/ar-culture'
    })
  },

  // 前往线索反馈
  goToFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },

  // 前往文化打卡
  goToCheckin() {
    wx.navigateTo({
      url: '/pages/checkin/checkin'
    })
  },

  // 前往建筑详情
  goToBuildingDetail(e: any) {
    const buildingId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/building-detail/building-detail?id=${buildingId}`
    })
  },

  // 前往资讯详情
  goToNewsDetail(e: any) {
    const newsId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/news-detail/news-detail?id=${newsId}`
    })
  }
})